#include "utils.h"
#include <pthread.h>

void *thread_func_dynamic(void *param) {
    threadParam_td *p = (threadParam_td *)param;
    int k = p->k;
    for (int i = k + p->t_id + 1; i < n; i += thread_count) {
        for (int j = k + 1; j < n; j++) A[i][j] -= A[i][k] * A[k][j];
        A[i][k] = 0;
    }
    pthread_exit(NULL);
    return NULL;
}

void dynamic_gauss() {
    for (int k = 0; k < n; k++) {
        float ele = A[k][k];
        for (int j = k + 1; j < n; j++) A[k][j] /= ele;
        A[k][k] = 1.0;
        pthread_t *handles = new pthread_t[thread_count];
        threadParam_td *param = new threadParam_td[thread_count];
        for (int t_id = 0; t_id < thread_count; t_id++) {
            param[t_id].k = k;
            param[t_id].t_id = t_id;
            pthread_create(&handles[t_id], NULL, thread_func_dynamic, &param[t_id]);
        }
        for (int t_id = 0; t_id < thread_count; t_id++) pthread_join(handles[t_id], NULL);
        delete[] handles;
        delete[] param;
    }
}
